//
//  SkyTabBarViewController.m
//  modao
//
//  Created by BlueSky on 2020/12/7.
//  Copyright © 2020 BlueSky. All rights reserved.
//

#import "SkyTabBarViewController.h"
#import "GWBaseTableViewController.h"

#import "GWHomeViewController.h"
#import "TestViewController.h"
#import "GWMineViewController.h"
#import "FindViewController.h"
#import <objc/runtime.h>

#import "GWCodeLoginViewController.h"

#import "SkyTabBar.h"


//#import "modao-Swift.h"


@interface SkyTabBarViewController ()<SkyTabBarDelegate>
@property (assign, nonatomic) BOOL isTest;
/**! bgImage*/
@property (nonatomic, strong) UIImageView *bgImage;


@end

@implementation SkyTabBarViewController

#pragma mark - 第一次使用当前类的时候对设置UITabBarItem的主题
+ (void)initialize
{
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    
    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
    dictNormal[NSForegroundColorAttributeName] = TEXT_COLOR_221;
    dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    
    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSForegroundColorAttributeName] = MAIN_ACTIVE_COLOR;
    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    
    if (@available(iOS 10.0, *)) {
        [[UITabBar appearance] setUnselectedItemTintColor:TEXT_COLOR_221];
    } else {
        // Fallback on earlier versions
    }
    
    [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpAllChildVc];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeState) name:@"ChangeTabbarState" object:nil];;

    //添加tabbar背景颜色
//    UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -33, SCREEN_WIDTH, 130)];
    UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(-15, -33, SCREEN_WIDTH +30, CUSTOM_TABBAR_HEIGHT)];
    backView.image = IMAGE_NAME(@"Icon_Tab_Base_Image");
//    backView.contentMode = UIViewContentModeScaleAspectFit;
//    backView.clipsToBounds = YES;
    self.bgImage = backView;
    [self.tabBar insertSubview:backView atIndex:0];

}


#pragma mark - ------------------------------------------------------------------
#pragma mark - 初始化tabBar上除了中间按钮之外所有的按钮

- (void)setUpAllChildVc
{
    
    GWHomeViewController *HomeVC = [[GWHomeViewController alloc] init];
    [self setUpOneChildVcWithVc:HomeVC Image:@"Icon_Tab Bar_Home_Inactive" selectedImage:@"Icon_Tab Bar_Home_Active" title:@"首页"];
    
    TestViewController *MessageVC = [[TestViewController alloc] init];
    [self setUpOneChildVcWithVc:MessageVC Image:@"Icon_Tab Bar_Test_Inactive" selectedImage:@"Icon_Tab Bar_Test_Active" title:@"测试"];
    
    FindViewController *ShopVC = [[FindViewController alloc] init];
    [self setUpOneChildVcWithVc:ShopVC Image:@"Icon_Tab Bar_Find_Inactive" selectedImage:@"Icon_Tab Bar_Find_Active" title:@"发现"];
    
    GWMineViewController *MineVC = [[GWMineViewController alloc] init];
    [self setUpOneChildVcWithVc:MineVC Image:@"Icon_Tab Bar_User_Inactive" selectedImage:@"Icon_Tab Bar_User_Active" title:@"我的"];
}

#pragma mark - 初始化设置tabBar上面单个按钮的方法

/**
 *  @author li bo, 16/05/10
 *
 *  设置单个tabBarButton
 *
 *  @param Vc            每一个按钮对应的控制器
 *  @param image         每一个按钮对应的普通状态下图片
 *  @param selectedImage 每一个按钮对应的选中状态下的图片
 *  @param title         每一个按钮对应的标题
 */
- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
{
    
    UIImage *myImage = [UIImage imageNamed:image];
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //tabBarItem，是系统提供模型，专门负责tabbar上按钮的文字以及图片展示
    Vc.tabBarItem.image = myImage;
    
    UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    Vc.tabBarItem.selectedImage = mySelectedImage;
    
    Vc.tabBarItem.title = title;
    
    Vc.navigationItem.title = title;
    if (self.viewControllers.count < 4) {
        GWBaseNavigationController *nav = [[GWBaseNavigationController alloc] initWithRootViewController:Vc];
        [self addChildViewController:nav];
    }
    
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
    
    
    NSInteger index = [tabBar.items indexOfObject:item];
    
    NSMutableArray *array = [NSMutableArray array];
    for (UIView *view in tabBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [array addObject:view];
        }
    }
    if (index != 1) {
        [self setGeneralTabbarInfo];
    }
    
    UIView *itemView = array[index];
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.5f;
    NSMutableArray *animationArray = [NSMutableArray array];
    [animationArray addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1)]];
    [animationArray addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1)]];
    [animationArray addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1)]];
    [animationArray addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1)]];
    animation.values = animationArray;
    
    [itemView.layer addAnimation:animation forKey:nil];
}


- (void)changeState {
    self.isTest = YES;
    [self setBlackTabbarInfo];
}

//常规
- (void)setGeneralTabbarInfo {
    self.isTest = NO;
    self.bgImage.image = IMAGE_NAME(@"Icon_Tab_Base_Image");
    [self setUpOneChildVcWithVc:[self creatOrfindControllerWithClass:GWHomeViewController.class] Image:@"Icon_Tab Bar_Home_Inactive" selectedImage:@"Icon_Tab Bar_Home_Active" title:@"首页"];
    
    [self setUpOneChildVcWithVc:[self creatOrfindControllerWithClass:TestViewController.class] Image:@"Icon_Tab Bar_Test_Inactive" selectedImage:@"Icon_Tab Bar_Test_Active" title:@"测试"];
    
    [self setUpOneChildVcWithVc:[self creatOrfindControllerWithClass:FindViewController.class] Image:@"Icon_Tab Bar_Find_Inactive" selectedImage:@"Icon_Tab Bar_Find_Active" title:@"发现"];
    
    [self setUpOneChildVcWithVc:[self creatOrfindControllerWithClass:GWMineViewController.class] Image:@"Icon_Tab Bar_User_Inactive" selectedImage:@"Icon_Tab Bar_User_Active" title:@"我的"];
    
//    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
//    dictNormal[NSForegroundColorAttributeName] = TEXT_COLOR_221;
//    dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:12];
//
//    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
//    dictSelected[NSForegroundColorAttributeName] = MAIN_ACTIVE_COLOR;
//    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:12];
//
//    if (@available(iOS 10.0, *)) {
//        [[UITabBar appearance] setUnselectedItemTintColor:TEXT_COLOR_221];
//    } else {
//        // Fallback on earlier versions
//    }
//
//    [self.tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
//    [self.tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];

}
///黑色
- (void)setBlackTabbarInfo {
    self.bgImage.image = IMAGE_NAME(@"Icon_Tab_Base_Image_block");
    
    [self setUpOneChildVcWithVc:[self creatOrfindControllerWithClass:GWHomeViewController.class] Image:@"Icon_Tab Bar_Home_black" selectedImage:@"Icon_Tab Bar_Home_black" title:@"首页"];
    
    [self setUpOneChildVcWithVc:[self creatOrfindControllerWithClass:TestViewController.class] Image:@"Icon_Tab Bar_Test_Inactive" selectedImage:@"Icon_Tab Bar_Test_Inactive" title:@"测试123"];
    
    [self setUpOneChildVcWithVc:[self creatOrfindControllerWithClass:FindViewController.class] Image:@"Icon_Tab Bar_Find_black" selectedImage:@"Icon_Tab Bar_Find_black" title:@"发现"];
    
    [self setUpOneChildVcWithVc:[self creatOrfindControllerWithClass:GWMineViewController.class] Image:@"Icon_Tab Bar_User_black" selectedImage:@"Icon_Tab Bar_User_black" title:@"我的"];
    
    self.tabBar.tintColor = UIColor.whiteColor;//选中

    self.tabBar.unselectedItemTintColor = MAIN_BACKCOLOR;

    
}

- (UIViewController *)creatOrfindControllerWithClass:(Class)cls {
    for (UINavigationController *nav in self.viewControllers) {
        if ([nav.viewControllers.firstObject isKindOfClass:cls]) {
            return nav.viewControllers.firstObject;
        }
    }
    return [cls new];
}

@end
